package com.moji.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.entity.*;
import com.moji.entity.dto.RouterrDto;
import com.moji.mapper.MjAboutUsMapper;
import com.moji.mapper.MjProductMapper;
import com.moji.service.MjAboutUsService;
import com.moji.service.MjProductService;
import org.apache.http.util.TextUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 产品表(MjProduct)表服务实现类
 *
 * @author makejava
 * @since 2024-07-21 14:36:36
 */
@Service("mjProductService")
public class MjProductServiceImpl extends ServiceImpl<MjProductMapper, MjProduct> implements MjProductService {
    @Resource
    private MjProductMapper mjProductDao;

    /**
     * 通过ID查询单条数据
     *
     * @return 实例对象
     */
    @Override
    public MjProduct queryById(RouterrDto routerrDto) {
        if(routerrDto.getId()==null)
        {
            if(!TextUtils.isEmpty(routerrDto.getRouter()))
            {
                LambdaQueryWrapper<MjProduct> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(MjProduct::getRouter,routerrDto.getRouter());
                queryWrapper.orderByAsc(MjProduct::getSort);
                queryWrapper.orderByDesc(MjProduct::getUpdateTime);
                List<MjProduct> page = this.list(queryWrapper);
                if(page==null||page.size()!=1)
                {
                    return null;
                }else{
                    return  page.get(0);
                }

            }
        }
        return this.mjProductDao.selectById(routerrDto.getId());
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<MjProduct> queryByPage() {
        LambdaQueryWrapper<MjProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(MjProduct::getSort);
        queryWrapper.orderByDesc(MjProduct::getUpdateTime);
        List<MjProduct> page = this.list(queryWrapper);
        return page;
    }

    /**
     * 新增数据
     *
     * @param mjProduct 实例对象
     * @return 实例对象
     */
    @Override
    public MjProduct insert(MjProduct mjProduct) {
        this.mjProductDao.insert(mjProduct);
        return mjProduct;
    }

    /**
     * 修改数据
     *
     * @param mjProduct 实例对象
     * @return 实例对象
     */
    @Override
    public MjProduct update(MjProduct mjProduct) {
        LambdaQueryWrapper<MjProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MjProduct::getId,mjProduct.getId());
        this.mjProductDao.update(mjProduct,queryWrapper);
        RouterrDto routerrDto=new RouterrDto();
        routerrDto.setId(mjProduct.getId());
        return this.queryById(routerrDto);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.mjProductDao.deleteById(id) > 0;
    }
}
